﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
using HYView;

namespace YJKArch.Aproll
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdCreateAproll : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			this.m_FirstShowFailureMessage = false;
			CmdViewType types = CmdViewType.FloorPlan;
			if (SwitchView.SetUsableView(types, commandData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (AprollDesigner._hWndRevit == null)
			{
				AprollDesigner._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			commandData.Application.Application.FailuresProcessing += this.OnFailuresProcessing;
			Result result = 0;
			try
			{
                //result = new AprollDesigner(commandData).CreateAproll(ref message);
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		private void OnFailuresProcessing(object sender, FailuresProcessingEventArgs e)
		{
			FailuresAccessor failuresAccessor = e.GetFailuresAccessor();
			string transactionName = failuresAccessor.GetTransactionName();
			IList<FailureMessageAccessor> failureMessages = failuresAccessor.GetFailureMessages();
			if (failureMessages.Count == 0)
			{
				e.SetProcessingResult((FailureProcessingResult)0);
				return;
			}
			if (transactionName.Equals("create aproll family"))
			{
				if (failureMessages.Count > 0 && !this.m_FirstShowFailureMessage)
				{
					MessageBox.Show("由于个别\"散水边线\"长度小于\"散水宽度\"，部分散水无法生成。", "盈建科提示");
					this.m_FirstShowFailureMessage = true;
				}
				foreach (FailureMessageAccessor failureMessageAccessor in failureMessages)
				{
					failuresAccessor.ResolveFailure(failureMessageAccessor);
				}
				e.SetProcessingResult((FailureProcessingResult)1);
				return;
			}
			e.SetProcessingResult((FailureProcessingResult)0);
		}

		private bool m_FirstShowFailureMessage;
	}
}
